<?php
namespace frontend\models;

use common\models\User;
use yii\base\Model;
use Yii;

/**
 * Signup form
 */
class SignupForm extends Model
{
    public $username;
    public $email;
    public $password;
    public $password2;
    public $phone;
    public $code;

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [

            ['phone', 'required'],
            ['phone', 'unique', 'targetClass' => '\common\models\User', 'message' => '该手机已被注册'],
            /*['phone', 'filter', 'filter' => function ($value) {
                // 在此处标准化输入的电话号码
                return $value;
            }],*/

            /*['username', 'filter', 'filter' => 'trim'],
            ['username', 'required'],
            ['username', 'unique', 'targetClass' => '\common\models\User', 'message' => 'This username has already been taken.'],
            ['username', 'string', 'min' => 2, 'max' => 255],*/

            /*['email', 'filter', 'filter' => 'trim'],
            ['email', 'required'],
            ['email', 'email'],
            ['email', 'string', 'max' => 255],
            ['email', 'unique', 'targetClass' => '\common\models\User', 'message' => 'This email address has already been taken.'],*/

            ['password', 'required'],
            ['password', 'string', 'min' => 6],

            ['password2', 'compare','compareAttribute'=>'password'],
        ];
    }

    /**
     * Signs user up.
     *
     * @return User|null the saved model or null if saving fails
     */
    public function signup()
    {
        if (!$this->validate()) {
            return null;
        }
        
        $user = new User();
        $user->phone = $this->phone;
        $user->setPassword($this->password);
        $user->email = '';
        $user->username = $this->phone;
        $user->real_name = '';
        $user->third_user_id = 0;
        $user->status = 10;
        $user->available_money = 0.00;
        $user->create_time = date('Y-m-d H:i:s');
        $user->reigster_ip = Yii::$app->request->userIP;
        $user->last_login_time = date('Y-m-d H:i:s');
        $user->last_login_ip = Yii::$app->request->userIP;
        /*
        var_dump($user->save());
        var_dump($user->errors);*/
        return $user->save() ? $user : null;
    }
}
